<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>类</title>
</head>

<body>

</body>
<script>
    // 创建一个Person类，其中实例属性包括：姓名name，年龄age。
    // 创建一个数组people，向该列表中存入4个Person的实例 按照年龄升序对people列表进行排序，显示排序后的姓名和年龄。
    // 为Person类添加一个方法setAttr(attr, value) ，可以动态的为Person类的实例添加属性和属性值

    /*  // es5实现
        function Person(name, age) {
            this.name = name;
            this.age = age;
        }
        Person.prototype.setAttr = function (attr, value) {
            this[attr] = value
        }
        let people = [];
        let s1 = new Person("tom", 12);
        let s2 = new Person("marry", 13);
        let s3 = new Person("jerry", 14);
        let s4 = new Person("xiaoming", 15);
        people.push(s1, s2, s3, s4)
        people.sort((a, b) => a.age - b.age)
        people[0].setAttr("sex","boy")
        console.log(people); */


    // es6实现
    class Person {
        constructor(name, age) {
            this.name = name;
            this.age = age;
        }
        setAttr(attr, value) {
            this[attr] = value
        }
    }
    let people = [];
    let s1 = new Person("tom", 12);
    let s2 = new Person("marry", 13);
    let s3 = new Person("jerry", 14);
    let s4 = new Person("xiaoming", 15);
    people.push(s1, s2, s3, s4)
    people.sort((a, b) => a.age - b.age)
    people[0].setAttr("sex", "boy")
    console.log(people);
</script>

</html>